
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
const api = require('../../utils/api.js')
const app = getApp()
Page({
  data: {
    tabs: ["客户列表", "推广订单"],
    activeIndex: '0',
    sliderOffset: 0,
    sliderLeft: 0,
    userId: '',
    couponList: [],
    page: 1,
    end: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    this.setData({
      activeIndex : options.actId+ ''
    })
  
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  
    if(options.actId === '0'){
      this.getCouponList('0');
    }else{
      
      this.getCouponList('1');
    }
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      couponList: [],
      page: 1,
      end: false
    })
    this.getCouponList(this.data.activeIndex, 1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var scroll_page = this.data.page;
    if (!this.data.end) {
      scroll_page++;
      this.setData({
        page: scroll_page
      })
      this.getCouponList(this.data.activeIndex, scroll_page, )
    } else {
      return wx.showToast({
        title: '没有更多了',
        icon: 'none',
        duration: 1500
    })
      
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id,
      page: 1,
      couponList: [],
      end:false
    });

    this.getCouponList(e.currentTarget.id)
  },
  /* 获取优惠券 */
  getCouponList: function (status, page) {
    wx.stopPullDownRefresh()
    var that = this;
    var lists = this.data.couponList;
    wx.showLoading({
      title: '加载中',
    })
    if(status === '0'){
      setTimeout(function () {
        api.post('distribution/front/user/inviter/list', {
          page: page || 1,
          pageSize: 10,
          userId: that.data.userId || wx.getStorageSync('user').userId+'',
          bid: app.globalData.shopId + '',
          shopId: app.globalData.shopId + ''
        }).then(res => {
          wx.hideLoading()
          if (res.data.success && res.data.data.list.length) {
            res.data.data.list.map(item => {
              var coupon_item = {};
              coupon_item.counterpartyUserMobile = item.counterpartyUserMobile;
              coupon_item.counterpartyUserName = item.counterpartyUserName;
              coupon_item.createTime = item.createTime;
             
              lists.push(coupon_item)
            })
            that.setData({
              couponList: lists
            })
           
          }
  
          if(that.data.page*10 >= res.data.data.total){
            that.setData({
              end: true
            })
          }
        })
  
      }, 500)
    }else{
      setTimeout(function () {
        api.post('distribution/front/user/order/list', {
          page: page || 1,
          pageSize: 10,
          userId: that.data.userId || wx.getStorageSync('user').userId+'',
          bid: app.globalData.shopId + '',
          shopId: app.globalData.shopId + ''
        }).then(res => {
          wx.hideLoading()
          if (res.data.success && res.data.data.list.length) {
            res.data.data.list.map(item => {
              var coupon_item = {};
              coupon_item.counterpartyUserMobile = item.counterpartyUserMobile;
              coupon_item.shareSn = item.shareSn;
              coupon_item.shareAmount = item.shareAmount/100;
              coupon_item.createTime = item.createTime;
              lists.push(coupon_item)
            })
            that.setData({
              couponList: lists
            })
            
          }
  
          if(that.data.page*10 >= res.data.data.total){
            that.setData({
              end: true
            })
          }
        })
  
      }, 500)
    }
  

  }
})